package leetcode.code0764;

import java.util.LinkedList;
import java.util.Queue;

import leetcode.IDebug;
import leetcode.helper.H;

/**
 * 暴力最后一个用例超时
 * 
 * @author CaoYu
 * @date 2022年8月5日
 * @description
 */
public class Solution2 implements IDebug {

	int n;

	public int orderOfLargestPlusSign(int n, int[][] mines) {
		this.n = n;
		int[][] grid = new int[n][n];
		for (int i = 0; i < mines.length; i++) {
			int[] mine = mines[i];
			grid[mine[0]][mine[1]] = 1;
		}
		int ans = Integer.MIN_VALUE;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (grid[i][j] == 0) {
					int cur = this.find(i, j, grid);
					ans = Math.max(ans, cur);
				}
			}
		}
		return ans == Integer.MIN_VALUE ? 0 : ans;
	}

	private int find(int x, int y, int[][] grid) {
		int ans = 1;
		Queue<int[]> queue = new LinkedList<>();
		queue.add(new int[] { x, y, 0, 1 });
		queue.add(new int[] { x, y, 0, -1 });
		queue.add(new int[] { x, y, 1, 0 });
		queue.add(new int[] { x, y, -1, 0 });
		while (!queue.isEmpty()) {
			int size = queue.size();
			while (size > 0) {
				int[] cur = queue.poll();
				int nx = cur[0] + cur[2], ny = cur[1] + cur[3];
				if (nx < 0 || nx == n || ny < 0 || ny == n || grid[nx][ny] == 1) {
					return ans;
				}
				queue.add(new int[] { nx, ny, cur[2], cur[3] });
				size--;
			}
			ans++;
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(2, this.orderOfLargestPlusSign(5, H.str2array1("[[4,2]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
